package com.api.gbhg.zengping.action;


import com.api.gbhg.lbj.dao.ReimbursementDao;
import com.api.gbhg.zengping.common.CommonDao;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: gbhg-oa
 * @author: Mr.zeng
 * @create: 2021-01-06 15:43
 *
 * 归档后扣减冻结金额Action
 *  * 修改状态 审核通过1 ,并对冻结金额做相应扣减
 **/
public class AmountAction implements Action {
    public static final Logger LOGGER = LoggerFactory.getLogger("gbhg");
    private   final PropertyManagementCostDao pmc=new PropertyManagementCostDao();
    private   final ReimbursementDao rei= new ReimbursementDao();

    @Override
    public String execute(RequestInfo requestInfo) {
        LOGGER.info("========================扣减冻结金额 AmountAction==============================================");
        String requestid = requestInfo.getRequestid();
        //标题
        String description = requestInfo.getDescription();
        //得到明细表名
        DetailTable[] detailTables = CommonDao.getDetailTables(requestInfo);
        //当前流程表名-类似 formtable_main_22
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = CommonDao.selectByRequestId(new RecordSet(), billTableName, requestid);
        rs.next();
        String mainid = rs.getString("id");
        RecordSet recordSet = CommonDao.selectByDetailTable(detailTables[0].getTableDBName(), mainid);
        Map<String, String>[] list = new HashMap[recordSet.getArray().size()];
        int i = 0;
        while (recordSet.next()){
            int count=0;
            list[i]=new HashMap<>();
            //部门经济支出分类
            String bmzcjjfl=recordSet.getString("bmjjzcfl");
            //预算项目id
            String ysxm = recordSet.getString("ysxm");
            //同意报销金额
            BigDecimal tybxje=new BigDecimal(recordSet.getDouble("tybxje"));
            //申请报销金额
            BigDecimal sqmoneyDj=new BigDecimal(recordSet.getDouble("sqbxje"));
            String zcqd = recordSet.getString("zcqd");
            String ysly = recordSet.getString("ysly");
            String fzysrfl = recordSet.getString("fzysrfl");
            String xzkmdm = recordSet.getString("xzkmdm");
            String bmjjzcfl = recordSet.getString("bmjjzcfl");
            String cxje = recordSet.getString("cxje");
            String bxr = recordSet.getString("bxr");
            String nd = recordSet.getString("nd");
            list[i].put("bxr",bxr);//报销人
            list[i].put("cxje",cxje);//冲销金额
            list[i].put("lcjlid", ysxm);//预算项目i
            list[i].put("money", String.valueOf(tybxje));//申请报销金额
            list[i].put("sqmoney", String.valueOf(sqmoneyDj));//申请报销金额
            list[i].put("bmzcjjfl", bmjjzcfl);//部门经济支出分类
            list[i].put("zcqd", zcqd);//支出渠道
            list[i].put("ysly", ysly);//预算来源
            list[i].put("fzysrfl", fzysrfl);//非中央收入分类
            list[i].put("xzkmdm", xzkmdm);//选择科目代码
            list[i].put("nd", nd);//选择科目代码
            i++;
            BigDecimal cxje1=new BigDecimal(cxje);
            //money/=10000;
            LOGGER.info("申请报销金额:"+sqmoneyDj+"同意报销金额:"+tybxje);
            // 扣减金额，将冻结金额置为0
            boolean b1 = pmc.modifyFreezeMoney(ysxm, bmzcjjfl,tybxje,cxje1,true);
            // 修改状态-bxlcmc-bxlcjlid
            boolean b = pmc.modifyJingfeiSpzt(ysxm,requestid,description);
            //添加报销流程明细记录
            Map<String,Object> data=new HashMap<>();
            //mainid
            data.put("mainid",mainid);
            //报销金额
            data.put("bxje",tybxje);
            //报销流程id
            data.put("bxlcid",requestid);
            //项目流程id
            data.put("xmlcid",ysxm);
            //项目流程名称  根据 ysxm 查==>查经费表
            RecordSet rsj=null;
            if(count==0){
                rsj = CommonDao.selectJingfei(ysxm);
                rsj.next();
            }
            //项目名称
            String xmmc = rsj.getString("xmmc");
            data.put("xmlcmc",xmmc);
            //部门经济支出分类
            data.put("bmjjzcfl",recordSet.getString("bmjjzcfl"));
            //政府经济支出分类
            data.put("zfjjzcfl",recordSet.getString("zfjjzcfl"));
            //备注
            data.put("bz","无备注");
            LOGGER.info("data----:"+data.toString());
            boolean b2 = pmc.addbxDetailRecord(data);
            count++;
            if(!b1||!b||!b2){
                LOGGER.info("============================归档后扣减金额出现错误，修改审批状态错误======================================");
                requestInfo.getRequestManager().setMessagecontent("归档后扣减金额出现错误，修改审批状态错误");
                requestInfo.getRequestManager().setMessageid("1");
                return FAILURE_AND_CONTINUE;
            }
        }
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        boolean b3 = rei.userQuotaFreezeDeduction(rst,list, true);
        if (b3){
            LOGGER.info("============================归档后扣减金额出现错误，修改审批状态错误======================================");
            requestInfo.getRequestManager().setMessagecontent("归档后扣减金额出现错误，修改审批状态错误");
            requestInfo.getRequestManager().setMessageid("1");
            return FAILURE_AND_CONTINUE;
        }
        rst.commit();
        return SUCCESS;
    }
}
